Zeichenketten und datumswerte formatieren
Oft komt es vor, dass man zeichenketten und datumswerte in einem bestimmten format ausgeben muss. das Microsoft framework stellt eine mächte methode zur Verf ügung mit der man einfach und
String. format is a new static method in JDK1.5, which has powerful functions. Its main function is to format data, which is roughly divided into these classes (general type, character type, numeric type, date type ). Its syntax is as follows:
Datetime dt = datetime. Now; //, January 1, October 4, 2010String STR = "";// STR = string. Format ("{0: y yy yyy yyyy}", DT); // 10 10 2010 2010// STR = string. Format ("{0: m mm mmm}", DT); // 10 May October 10// STR = string. Format ("{0: d dd
String. Format (C #, VB. NET)
I used:
String. Format ("string: {0: D5}", 12)"String: 00012" 'occupies 5 places. If not, use 0 instead.String. Format ("string: {0, 5}", 12)"String: 12" 'occupies 5 places. If not, use spaces instead. The right
1. Escape
C # uses placeholders such as {0}, {1: yyyy-MM-dd}. What if the formatted string itself contains {or? The answer is: Use two {or} hyphens to represent a single one.
For exampleStringStr1 = String. Format ("{Hello}, {0}, Now is {1:
string. Five types of overloads for format:
String. Format (String,object) replaces one or more formatting items in the specified string with the string representation of the specified object.String. The format (string,object,object) in the
1. format the currency (related to the system environment. The Chinese system uses the default format of RMB and the English system uses the format of USD)
String. Format ("{0: c}", 0.2): ¥0.20 (English OS result: $0.20)
By default, two decimal
Conditional operators (? :) And $ "replace string. Format (), string. format
1. Conditional operators (? :) Return one of the two values based on the value of the Boolean expression. The expression is as follows:
Condition? First_expression:
Date string format conversion, string format conversion
Date format conversion is a common problem during development.
For example, the date format is 13.06.2017, June 13, 2017, June 13, 2017, and.
You can use string truncation to format a date in a
String. format () format string, String format string
1. Several Common conversion Operators
Description of the conversion operator % d Integer type (decimal) 99% f floating point type 99.99% s string type "mingrisoft" % c character type 'M' % B
Import java. util. date;
Import java. util. locale;
Public class example {
Public static void main (string [] ARGs)
{
Date today = new date ();
String A = string. Format (locale. Us, "% TB", today); // The formatted string is the abbreviation of the
The string class has a very useful static method string. Format ():
Format (locale L, string format, object... ARGs) returns a formatted string using the specified language environment, format string, and parameters.
Format (string format, object...
Adding method of braces in String. format, large in string. format
String. Format ("{0} world! "," Hello ") // output hello world !, No problem, but you only need to add a braces at any position of the first parameter:String. Format ("{0} wo {rld! "
Quick Start introduction to the powerful Java String. format (), javastring. format
Preface
Since Java 5.0, the String class has added a powerful String formatting method format (). There are still not many people using this method till now, which
Public class StringFormat {Public static void main (String [] args ){Method ("aa", 124 ); } Public static void method (String a, int B ){// Result:/inner/payment/aa/courier/124String s = String. format ("/inner/payment/% s/courier/% d", a, B );
This is often used to format strings of dates and numbers, so we sorted out some of the help items here.
The following table describes the standard format specifiers used to format datetime objects.Format descriptionD short date mode D. Long date
Java magic Hall: String. formatI. Preface String. as a text processing tool, format provides us with powerful and rich String formatting functions. format ("Hello % s", "John");. sort and record the notes below. Ii. overload method // use the
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.